<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  checked: "",
  mixins: [Vue.prototype.$vFN.Mixins.Base,Vue.prototype.$vFN.Tools],
  data() {
    return {
      load: true,
      // value: false,
      dialogVisible: false,
      //数据表单
      form: {
        buyerMessage: "",
        id: 0,
        isOnlyRefund: 0,
        isOnlyRefundProduct: 0,
        orderAutoCloseTime: 0,
        orderAutoReceiveTime: 0,
        orderCloseRefundTime: 0,
        platformId: 0,
        rightsReasonSettingLst: [],
        // 售后原因
        showRightsReason: 0,
        // 规格库存不足处理方式
        underStockDealType: 1,
        defaultPayMethodType: 1, // 默认支付方式 1优先选中线上支付 2优先选中线下支付
      },
      //创建理由数据
      orderSettingId: "",
      reason: "",
    };
  },
  methods: {
    async init(type_) {
      this.load = true;
      // 获取数据
      let _data = await this.$vFN.Http.post(
        {
          url: "mall-site-portal/order-setting/getItem",
        },
        window
      );
      if (_data._ok) {
        if (type_) {
          this.form.rightsReasonSettingLst = _data.data.rightsReasonSettingLst;
        } else {
          this.orderSettingId = _data.data.id;
          // console.error(_data.data,this.form)
          for (let i in this.form) {
            this.form[i] = _data.data[i];
          }
        }
      }
      this.load = false;
    },
    // 验证
    validate() {
      // 表单验证
      return new Promise((resolve, reject) => {
        this.$refs["editForm"].validate((valid) => {
          valid ? resolve(true) : resolve(false);
        });
      });
    },

    // 验证填写整数
    validatorNumber(rule, value, callback){

       if (value != 0 &&  this.$vFN.Utils.isEmpty(value)) {
        callback(new Error("不能为空"));
      }  else {
        // 只能填正整数
        this.$vFN.Tools.validateStock(rule, value, callback);
      }

    },


    // 提交
    async submit() {
      if (!(await this.validate())) {
        return false;
      }
      this.load = true;
      //更新数据
      let _data = await this.$vFN.Http.post(
        {
          url: "mall-site-portal/order-setting/saveOrUpdate",
          data: this.form,
        },
        window
      );
      if (_data._ok) {
        this.$vFN.Tools.messageSuccess("操作成功");
        this.init();
      }
      this.load = false;
    },

    async createReason() {
      //创建原因
      if (this.reason == "") {
        this.$vFN.Tools.messageError("内容不能为空");
        return true;
      }
      let _data = await this.$vFN.Http.post(
        {
          url: "mall-site-portal/order-setting/createReason",
          data: {
            reason: this.reason,
            orderSettingId: this.orderSettingId,
          },
        },
        window
      );
      if (_data._ok) {
        this.dialogVisible = false;
        this.init(true);
      }
    },
    //删除原因
    deleteReason(item_) {
      this.$confirm("确定删除  <strong>" + item_.reason + "</strong>么", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        // type: "error",
        dangerouslyUseHTMLString: true,
      })
        .then(async () => {
          let _data = await this.$vFN.Http.post(
            {
              url: "mall-site-portal/order-setting/deleteReason",
              data: {
                ids: [item_.id],
              },
            },
            window
          );
          if (_data._ok) {
            this.init(true);
          }
        })
        .catch(() => {});
    },
  },
  mounted() {
    this.init();
  },
};
</script>